package com.example.cinema.drawview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.example.cinema.R;

import static android.view.MotionEvent.ACTION_DOWN;

//折叠文字 折叠过长消息
public class CollapseText extends View {
    public CollapseText(Context context) {
        this(context, null);
    }

    public CollapseText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CollapseText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    private String textcontext;
    private int maxLines;
    private float textsize;
    private int textColor;

    public CollapseText(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCollapse, defStyleAttr, 0);
        this.textcontext = array.getString(R.styleable.MyCollapse_android_text);
        this.maxLines = array.getInt(R.styleable.MyCollapse_android_maxLines, 3);
        this.textsize = array.getDimension(R.styleable.MyCollapse_android_textSize, 16);
        this.textColor = array.getColor(R.styleable.MyCollapse_android_textColor, Color.parseColor("#000000"));
        init();
    }

    private int textnumb;
    private Paint paint;
    private char[] chars;
    private Paint textPaint;

    private void init() {
        if(textcontext!=null){
            this.chars = textcontext.toCharArray();
            this.textnumb = this.textcontext.length();
            this.lines = chars.length;
        }


        paint = new Paint();
        paint.setTextSize(textsize);
        paint.setColor(textColor);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setTypeface(Typeface.MONOSPACE);
        paint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.parseColor("#1E90FF"));
        textPaint.setTextSize(textsize);
        textPaint.setTypeface(Typeface.MONOSPACE);
        textPaint.setAntiAlias(true);
    }

    public void setTextcontext(String text)
    {
        this.textcontext = text;
    }


    private int width;
    private int heigh;
    private int lines;
    private int marg;

    private int textline;
    private int linesmagring = 20;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        width = widthMeasureSpec;
        heigh = heightMeasureSpec;
        width = resolveSize(width, widthMeasureSpec);
        heigh = resolveSize(heigh, heightMeasureSpec);


        if (width > 0) {
            this.lines = (int) (width / textsize);//单行文字数
            this.marg = (int) (width % textsize) / 2;//余数除2
            this.textline = textnumb / lines;//计算总行数
            if (textnumb % lines > 0) {
                this.textline++;
            }

        }

        if(islinestate == false)
        {
            if(textline<maxLines&&textline>0)
            {

                heigh = (int) (textline * textsize+(textline+1)*linesmagring+3);
            }else {
                heigh = (int) (maxLines * textsize+(maxLines+1)*linesmagring+3);
            }
        }else
        {
            heigh = (int) (this.textline * textsize+ (this.textline+1) * linesmagring+3);
            if(lintener!=null)
            {
                lintener.heightvariety(heigh);
            }
        }

        setMeasuredDimension(width,heigh);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private boolean islinebool;
    private boolean islinestate;

    @Override
    protected void onDraw(Canvas canvas) {
        if(textcontext!=null){
            this.chars = textcontext.toCharArray();
            this.textnumb = this.textcontext.length();
        }else {
            return;
        }
        if (this.textline > maxLines && islinestate == false) {

            islinebool = true;
        } else {
            islinebool = false;
        }

        int x = 0;
        int y = 0;
        int linewidth = width;

        int line = 0;
        int list = 1;
        for (int i = 0; i < chars.length; i++) {
            x = (int) (line * textsize + marg);

            y = (int) (list * textsize + linesmagring * list);
            if (linewidth - x <= textsize) {
                line = 0;
                x = (int) (line * textsize + marg);
                list++;
                y = (int) (list * textsize + linesmagring * list);
            }
            line++;


            if (islinebool && list >= maxLines && line >= lines - 2) {
                canvas.drawText("展开", x, y, textPaint);
                break;
            }


            canvas.drawText(String.valueOf(chars[i]), x, y, paint);
        }
        setMeasuredDimension(width, y);
        requestLayout();
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int ev = event.getActionMasked();
        if (ev == ACTION_DOWN) {

            float x = event.getRawX();
            float y = event.getRawY();
            if(x > width-textsize*2 && y > heigh-textsize*2)
            {
                if (!islinestate && textline > 3) {

                    islinestate = true;
                } else {
                    islinestate = false;
                }
                invalidate();
                requestLayout();
            }
        }

        return true;
    }

    private heightlintener lintener;
    public void setHeightLintener(heightlintener lintener)
    {
        this.lintener =  lintener;
    }

    public interface heightlintener{
        void heightvariety(int height);
    }
}
